home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / utility / 54 / utl / toascii.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-09-18  |  3.9 KB  |  146 lines

  1. /*
  2.  *-----------------------------------------------------------------------------
  3.  * toascii - Strip a file of all chars that are not printable
  4.  *  except CRLF. Expands tabs.
  5.  *-----------------------------------------------------------------------------
  6.  *
  7.  * (c) Copyright 1986 by Daniel C. Brotherton  All Rights Reserved
  8.  *
  9.  *   May be used for any personal, nonprofit purpose. Commercial users or
  10.  *    anyone with questions/comments may contact me at:
  11.  *         GEnie  D.BROTHERTON
  12.  *         Delphi LOBODAN
  13.  *         CIS    74746,3307
  14.  *
  15.  *   Written using Alcyon C v. 4.14 (updated version from Atari)
  16.  *
  17.  *-----------------------------------------------------------------------------
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include <ctype.h>
  22.  
  23. #define DEFTABS 8L
  24.  
  25. long tabval = DEFTABS;
  26.  
  27. main(argc, argv)
  28.  
  29. int argc;
  30. char *argv[];
  31.  
  32. {
  33.     long count,                                 /* char # in line      */
  34.          to_tab;                                /* spaces to next tab  */
  35.     char c;                                     /* input holder        */
  36.  
  37.     parse(argc, argv);
  38.  
  39.     while((c = getchar()) != EOF){
  40.         if (isprint(c)){                        /* if printable, print */
  41.             putchar(c);
  42.             count++;                            /* and add to total    */
  43.         }else if (c == '\n'){                   /* if CRLF put it out  */
  44.             putchar('\n');
  45.             count = 0L;                         /* reset char counter  */
  46.         }else if (c == '\t'){                   /* if tab, expand it   */
  47.             to_tab = tabval - (count % tabval);
  48.             spaces((int)to_tab);
  49.             count += to_tab;
  50.         }else{                                  /* replace with blank  */
  51.             putchar(' ');
  52.             count++;                            /* and add to total    */
  53.         }
  54.     }
  55.     out();
  56. }
  57.  
  58.  
  59.  
  60. /*
  61.  *---------------------------------------------------------------------------
  62.  * Parse input and set control values
  63.  *---------------------------------------------------------------------------
  64.  */
  65. static VOID    parse(argc, argv)
  66.  
  67. #define MINTAB 01
  68. #define MAXTAB 80
  69.  
  70. int argc;
  71. char *argv[];
  72.  
  73. {
  74.  
  75.     int i;
  76.  
  77.     if (argc < 1)
  78.         return;                                 /* no options selected */
  79.  
  80.     for (i = 1; i < argc; ++i) {                /* parse out switches */
  81.         if (argv[i][0] != '-')
  82.            parse_er(argv[i]);
  83.  
  84.         switch (toupper(argv[i][1])) {
  85.             case 'T':                           /* Set tab value */
  86.                 tabval = atol(&argv[i][2]);
  87.                 if ((tabval < MINTAB) || (tabval > MAXTAB))
  88.                     parse_er(argv[i]);
  89.                 break;
  90.             default:                            /* Invalid entry */
  91.                 parse_er(argv[i]);
  92.                 break;
  93.         }
  94.     }
  95. }
  96.  
  97.  
  98.  
  99. /*
  100.  *---------------------------------------------------------------------------
  101.  *  Output error messages
  102.  *---------------------------------------------------------------------------
  103.  */
  104. static VOID    parse_er(err_strg)
  105.  
  106. char *err_strg;
  107.  
  108. {
  109.     puts("ERROR: invalid entry");
  110.     spaces(4);
  111.     puts(err_strg);
  112.     puts("Usage: toascii [-T#]");
  113.     puts("       -T  = set tab value          (default:       08)");
  114.     out();
  115. }
  116.  
  117.  
  118.  
  119. /*
  120.  *---------------------------------------------------------------------------
  121.  * Program exit - call to here never returns
  122.  *---------------------------------------------------------------------------
  123.  */
  124. static VOID    out()
  125. {
  126.     exit(0);
  127. }
  128.  
  129.  
  130.  
  131. /*
  132.  *---------------------------------------------------------------------------
  133.  *  Output specified number of spaces
  134.  *---------------------------------------------------------------------------
  135.  */
  136. static VOID    spaces(i)
  137.  
  138. int i;
  139.  
  140. {
  141.     int j;
  142.  
  143.     for (j = 0; j < i; ++j)
  144.         putchar(' ');
  145. }
  146. əəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəəə